<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 8.9: Robust linear discrimination problem</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/robust_lin_discr.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 8.9: Robust linear discrimination problem</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.6.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original by Lieven Vandenberghe</span>
<span class="comment">% Adapted for CVX by Joelle Skaf - 10/16/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% The goal is to find a function f(x) = a'*x - b that classifies the points</span>
<span class="comment">% {x_1,...,x_N} and {y_1,...,y_M} with maximal 'gap'. a and b can be</span>
<span class="comment">% obtained by solving the following problem:</span>
<span class="comment">%           maximize    t</span>
<span class="comment">%               s.t.    a'*x_i - b &gt;=  t     for i = 1,...,N</span>
<span class="comment">%                       a'*y_i - b &lt;= -t     for i = 1,...,M</span>
<span class="comment">%                       ||a||_2 &lt;= 1</span>

<span class="comment">% data generation</span>
n = 2;
randn(<span class="string">'state'</span>,3);
N = 10; M = 6;
Y = [1.5+1*randn(1,M); 2*randn(1,M)];
X = [-1.5+1*randn(1,N); 2*randn(1,N)];
T = [-1 1; 1 1];
Y = T*Y;  X = T*X;

<span class="comment">% Solution via CVX</span>
cvx_begin
    variables <span class="string">a(n)</span> <span class="string">b(1)</span> <span class="string">t(1)</span>
    maximize (t)
    X'*a - b &gt;= t;
    Y'*a - b &lt;= -t;
    norm(a) &lt;= 1;
cvx_end

<span class="comment">% Displaying results</span>
linewidth = 0.5;  <span class="comment">% for the squares and circles</span>
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
tt = linspace(t_min-1,t_max+1,100);
p = -a(1)*tt/a(2) + b/a(2);
p1 = -a(1)*tt/a(2) + (b+t)/a(2);
p2 = -a(1)*tt/a(2) + (b-t)/a(2);

graph = plot(X(1,:),X(2,:), <span class="string">'o'</span>, Y(1,:), Y(2,:), <span class="string">'o'</span>);
set(graph(1),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'MarkerFaceColor'</span>,[0 0.5 0]);
hold <span class="string">on</span>;
plot(tt,p, <span class="string">'-r'</span>, tt,p1, <span class="string">'--r'</span>, tt,p2, <span class="string">'--r'</span>);
axis <span class="string">equal</span>
title(<span class="string">'Robust linear discrimination problem'</span>);
<span class="comment">% print -deps linsep.eps</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling SDPT3: 20 variables, 5 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  5
 dim. of socp   var  =  3,   num. of socp blk  =  1
 dim. of linear var  = 17
 number of nearly dependent constraints = 1
 To remove these constraints, re-run sqlp.m with OPTIONS.rmdepconstr = 1.
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|1.7e+02|2.5e+01|2.1e+03| 3.464102e+00  0.000000e+00| 0:0:00| chol  1  1 
 1|0.936|0.836|1.1e+01|4.2e+00|1.7e+02| 9.334394e+00 -1.024335e+01| 0:0:00| chol  1  1 
 2|1.000|1.000|1.5e-06|1.0e-02|1.4e+01| 4.666412e+00 -9.694230e+00| 0:0:00| chol  1  1 
 3|0.862|0.893|2.2e-07|2.0e-03|1.8e+00| 1.649135e+00 -1.964725e-01| 0:0:00| chol  1  1 
 4|0.742|1.000|6.2e-08|1.0e-04|1.0e+00| 8.971670e-01 -1.371686e-01| 0:0:00| chol  1  1 
 5|0.974|0.909|6.2e-09|1.8e-05|1.7e-01| 6.131286e-01  4.465455e-01| 0:0:00| chol  1  1 
 6|0.602|1.000|5.4e-09|1.0e-06|7.4e-02| 5.551363e-01  4.808752e-01| 0:0:00| chol  1  1 
 7|0.988|0.961|1.5e-09|1.4e-07|5.6e-03| 5.146807e-01  5.090810e-01| 0:0:00| chol  1  1 
 8|0.988|0.984|5.3e-10|1.2e-08|7.6e-05| 5.112718e-01  5.111961e-01| 0:0:00| chol  1  1 
 9|0.985|0.965|1.8e-10|5.4e-10|1.8e-06| 5.112306e-01  5.112287e-01| 0:0:00| chol  1  1 
10|1.000|0.994|2.7e-11|3.9e-11|1.4e-07| 5.112300e-01  5.112299e-01| 0:0:00| chol  1  1 
11|1.000|0.997|2.5e-12|5.5e-12|1.8e-09| 5.112299e-01  5.112299e-01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 11
 primal objective value =  5.11229899e-01
 dual   objective value =  5.11229898e-01
 gap := trace(XZ)       = 1.75e-09
 relative gap           = 8.67e-10
 actual relative gap    = 8.63e-10
 rel. primal infeas     = 2.50e-12
 rel. dual   infeas     = 5.49e-12
 norm(X), norm(y), norm(Z) = 1.1e+00, 1.2e+00, 8.5e+00
 norm(A), norm(b), norm(C) = 1.7e+01, 2.0e+00, 2.0e+00
 Total CPU time (secs)  = 0.13  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 2.5e-12  0.0e+00  5.5e-12  0.0e+00  8.6e-10  8.7e-10
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.51123
 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="robust_lin_discr__01.png" alt=""> 
</div>
</div>
</body>
</html>